package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * Content
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerContent extends Segment {
    private String $name;
    private String $example;

    public SwaggerContent(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $name = PropertiesConstants.getRaw(p,"name",$name);
        $example = PropertiesConstants.getRaw(p,"example",$example);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String name = PropertiesConstants.transform(ctx,$name,"application/json");
        if (StringUtils.isNotEmpty(name)) {
            XsObject property = current.getObjectChild("content",true);
            XsObject newObject = property.getObjectChild(name,true);
            newObject.addProperty("example",PropertiesConstants.transform(ctx,$example,""));
            super.onExecute(root, newObject, ctx, watcher);
        }
    }
}